#!/usr/bin/python
# -*- coding: utf8 -*-
from os import listdir
from os.path import exists

def choosewithnumber(list, message='Entrez unnuméro',ignore=[],index=0):
    output = {}
    i = 0
    if (type(list) == type("")):
        if (exists(list)):
            listing=listdir(list)
        else:
            print "Error %s is not a directory" % list
            return
    elif (type(list) == type([])):
        listing=list
    else:
        return

    for item in listing:
        if (type(item) == type(())):
            item=item[index]
        try:
            ignore.index(item)
        except ValueError:
            i+=1
            print "%i \t %s" % (i, item)
            output[i]=item            
    while 1:
        entry = raw_input(message)
        if (entry == "q"):
            return
        try:
            entryint=int(entry)-1
        except:
            print "Enter a number or q for quit"
            continue
        if ((entryint > i) or (entryint < 0)):
            print "Non valid"
        else:
            return listing[entryint]